#include "As2Variable.h"

namespace LightPlayer{

	As2Var::As2Var(){
		init();
	}

	As2Var::As2Var( const As2Var& other ){
		m_type = other.m_type;
		m_data = other.m_data;
		m_NewStrData.clear();
		if( other.m_type == eString && other.m_NewStrData.empty() == false ){
			m_NewStrData = other.m_NewStrData;
			m_data.str = m_NewStrData.c_str();
		}
	}

	void As2Var::init(){
		m_type = eNull;
		m_data.def = NULL;
		m_NewStrData.clear();
	}

	float As2Var::getAsFloat() const{
		if(m_type == eFloat){
			return m_data.f;
		}
		else if(m_type == eDouble){
			float f = (float) m_data.d;
			return f;
		}
		else if(m_type == eInt){
			float f = (float) m_data.i;
			return f;
		}
		else if(m_type == eString){
			float f = (float) atof( m_data.str );
			return f;
		}
		else if(m_type == eBool){
			return m_data.b ? 1.0f : 0.0f;
		}
		return 0.0f;
	}

	const char* As2Var::getAsString() const{
		if(m_type == eString){
			return m_data.str;
		}
		return NULL;
	}

	int As2Var::getAsInt() const {
		if(m_type == eFloat){
			int i = (int) m_data.i;
			return i;
		}
		else if(m_type == eDouble){
			int i = (int) m_data.d;
			return i;
		}
		else if(m_type == eInt){
			int i = (int) m_data.i;
			return i;
		}
		else if(m_type == eString){
			int i = (int) strtol(m_data.str,NULL,10);//atoi( m_data.str );
			return i;
		}
		else if(m_type == eBool){
			return m_data.b ? 1 : 0;
		}

		return 0;
	}

	bool As2Var::getAsBool()const {
		int int_val = getAsInt();
		if( int_val != 0 ){
			return true;
		}
		else if(m_type == eString){
			return !(strcmp( m_data.str, "false" ) == 0);
		}
		return false;
	}

};